package com.vecspace.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class NumberUtil {
	/**检查num是否为null或者0
	 * 
	 * @param num
	 * @return 
	 * @author: wanghua
	 * @reviewer: 
	 */
	public static boolean isNotNullZero(Long num) {
		return num != null && num.longValue() != 0;
	}
	
	public static boolean isNullOrZero(Long num) {
		return num == null || num.longValue() == 0;
	}
	
	/**检查num是否为null或者0
	 * 
	 * @param num
	 * @return 
	 * @author: wanghua
	 * @reviewer: 
	 */
	public static boolean isNotNullZero(Integer num) {
		return num != null && num.intValue() != 0;
	}	
	public static boolean isNullOrZero(Integer num) {
		return num == null || num.intValue() == 0;
	}
	
	public static boolean equals(Long num1, Long num2) {
		if(num1 == null || num2 == null)return false;
		return num1.equals(num2);
	}
	
	public static boolean equals(Integer num1, Integer num2) {
		if(num1 == null || num2 == null)return false;
		return num1.equals(num2);
	}
	

	/**判断两个数是否相等，0和null被认为相等
	 * 
	 * @param value1
	 * @param value2
	 * @param dx 如果 value1 和 value2的差值的绝对值小于dx，则认为相等
	 * @return 
	 * @author: wanghua
	 */
	public static boolean equals(BigDecimal value1, BigDecimal value2, double dx) {
		if(value1 == null && value2 == null) return true;
		BigDecimal dx2 = subtract(value1, value2);
		if(dx2.abs().doubleValue() < dx)return true;
		else return false;
	}
	
	/**判断两个数是否相等，存在null则不相等
	 * 
	 * @param value1
	 * @param value2
	 * @param dx
	 * @return 
	 * @author: wanghua
	 */
	public static boolean equalsNotNull(BigDecimal value1, BigDecimal value2, double dx) {
		if(value1 == null || value2 == null) return false;
		BigDecimal dx2 = value1.subtract(value2);
		if(dx2.abs().doubleValue() < dx)return true;
		else return false;
	}
	
	/**判断value1是否大于value2, null相当于0
	 * 
	 * @param value
	 * @return 
	 * @author: wanghua
	 */
	public static boolean greaterThan(BigDecimal value1, BigDecimal value2) {
		if(value1 == null) value1 = BigDecimal.ZERO;
		if(value2 == null) value2 = BigDecimal.ZERO;
		double dx = value1.subtract(value2).doubleValue();
		return dx > 0.000000001;
	}

	/**判断value1是否大于或者等于value2, null相当于0
	 * 
	 * @param value
	 * @return 
	 * @author: wanghua
	 */
	public static boolean greaterOrEqual(BigDecimal value1, BigDecimal value2) {
		if(value1 == null) value1 = BigDecimal.ZERO;
		if(value2 == null) value2 = BigDecimal.ZERO;
		double dx = value1.subtract(value2).doubleValue();
		return dx > -0.000000001;
	}
	/**判断value是否大于0
	 * 
	 * @param value
	 * @return 
	 * @author: wanghua
	 */
	public static boolean greaterZero(BigDecimal value) {
		if(value == null)return false;		
		return value.doubleValue() > 0.000000001;
	}
	
	public static boolean greaterOrEqualZero(BigDecimal value) {
		if(value == null)return false;		
		return value.doubleValue() > -0.000000001;
	}
	
    /**判断Long值是否大于0
     * 
     * @param v
     * @return 
     * @author: wanghua
     */
	public boolean greaterZero(Long num){
		if(num != null){
			return num.longValue() > 0;
		}else {
			return false;
		}
	}
	
	public static BigDecimal newBigDecimal(String s) {
		if(s == null || s.trim().length() == 0)return null;
		String numStr = s.trim();
		numStr = numStr.replace(",", "");
		numStr = numStr.replace("，", "");
		numStr = numStr.replace("_", "");
		BigDecimal d = new BigDecimal(numStr);
		return d;
	}
	/**两个数相除  decimal1 / decimal2, 默认最多保留6位小数
	 * 如果其中一个数为null，则返回null。如果decimal2为0，返回null
	 * @param decimal1
	 * @param decimal2
	 * @return 
	 * @author: wanghua
	 */
	public static BigDecimal divide(BigDecimal decimal1, BigDecimal decimal2) {
		if(decimal1 == null || decimal2 == null)return null;
		if(decimal2.equals(BigDecimal.ZERO)) return null;// 0 not equals 0.00
		if(decimal2.abs().doubleValue() < 0.00000000001)return null;

		int scale1 = decimal1.scale();
		int scale2 = decimal2.scale();
		int scale = scale1 > scale2 ? scale1 : scale2;
		BigDecimal result = decimal1.divide(decimal2, scale, RoundingMode.HALF_UP);//BigDecimal.ROUND_HALF_UP
		return result;
	}
	
	/**两个数相除  decimal1 / decimal2
	 * 如果其中一个数为null，则返回null。如果decimal2为0，返回null
	 * @param decimal1
	 * @param decimal2
	 * @param scale 保留的小数位
	 * @return 
	 * @author: wanghua
	 */
	public static BigDecimal divide(BigDecimal decimal1, BigDecimal decimal2, int scale) {
		if(decimal1 == null || decimal2 == null)return null;
		if(decimal2.equals(BigDecimal.ZERO)) return null;// 0 not equals 0.00
		if(decimal2.abs().doubleValue() < 0.00000000001)return null;
		BigDecimal result = decimal1.divide(decimal2, scale, RoundingMode.HALF_UP);//BigDecimal.ROUND_HALF_UP
		return result;
	}
	
	/**两个数相乘，其中一个数为null，则返回null
	 * 
	 * @param decimal1
	 * @param decimal2
	 * @return 
	 * @author: wanghua
	 */
	public static BigDecimal multiply(BigDecimal decimal1, BigDecimal decimal2) {
		if(decimal1 == null || decimal2 == null)return null;
		BigDecimal result = decimal1.multiply(decimal2);
		return result;
	}
	
	/**两个数相乘，其中一个数为null，则返回null
	 * 
	 * @param decimal1
	 * @param decimal2
	 * @param scale 保留小数位(四色五人)
	 * @return 
	 * @author: wanghua
	 */
	public static BigDecimal multiply(BigDecimal decimal1, BigDecimal decimal2, int scale) {
		if(decimal1 == null || decimal2 == null)return null;
		BigDecimal result = decimal1.multiply(decimal2);
		result.setScale(scale, RoundingMode.HALF_UP);
		return result;
	}
	
	/**两个数相加，如果其中一个数为null，则返回另一个数
	 * 
	 * @param decimal1
	 * @param decimal2
	 * @return 
	 * @author: wanghua
	 */
	public static BigDecimal add(BigDecimal decimal1, BigDecimal decimal2) {
		if(decimal1 == null)return decimal2;
		if(decimal2 == null)return decimal1;
		BigDecimal result = decimal1.add(decimal2);		
		return result;
	}
	
	/**两个数相减，如果decimal1为null，则返回 -decimal2,如果decimal2为null，则返回decimal1;
	 * 
	 * @param decimal1
	 * @param decimal2
	 * @return 
	 * @author: wanghua
	 */
	public static BigDecimal subtract(BigDecimal decimal1, BigDecimal decimal2) {
		if(decimal1 == null) {
			if(decimal2 != null) {
				return BigDecimal.ZERO.subtract(decimal2);
			}else {
				return null;
			}
		}
		if(decimal2 == null)return decimal1;
		BigDecimal result = decimal1.subtract(decimal2);		
		return result;
	}
	
}
